package cn.pzhu.javaee.mybook.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.pzhu.javaee.mybook.context.MybookConstant;
import cn.pzhu.javaee.mybook.dao.DetailDao;
import cn.pzhu.javaee.mybook.domain.Detail;
import cn.pzhu.javaee.mybook.domain.User;
import cn.pzhu.javaee.webapp.exception.WebAppBusinessException;
import cn.pzhu.javaee.webapp.servlet.WebAppBaseServlet;

/**
 * Servlet implementation class MyBookServlet
 */
@WebServlet("/mybook")
public class MyBookServlet extends WebAppBaseServlet {
	private static final long serialVersionUID = 1L;
    private DetailDao detailDao=new DetailDao();
	public void actionList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		log.debug("actionList calling");
		int curUserId=getCurUserId(request);
		List<Detail> detailList=detailDao.listByUserId(curUserId);
		request.setAttribute("DETAIL_LIST", detailList);
		this.forward(request, response, "mybook/list.jsp");
	}
	//mybook?action=page
	public void actionPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		log.debug("actionPage calling");
		int curUserId=getCurUserId(request);
		int p=1;
		int limit=10;
		if(request.getParameter("p")!=null){
			p=this.getInt(request, "p");
		}
		if(request.getParameter("limit")!=null){
			limit=this.getInt(request, "limit");
		}
		log.debug("p={},limit={}",p,limit);
		int startIndex=(p-1)*limit;
		long detaiLCount=detailDao.countOfDetailByUserId(curUserId);
		int pageCount=(int)detaiLCount/limit+(detaiLCount%limit==0?0:1);
		List<Detail> detailList=detailDao.pageByUserId(startIndex, limit,curUserId);
		request.setAttribute("DETAIL_LIST", detailList);
		request.setAttribute("CUR_PAGE", p);
		request.setAttribute("PAGE_COUNT", pageCount);
		request.setAttribute("LIMIT", limit);
		
		this.forward(request, response, "mybook/page.jsp");
	}
	public void actionAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.forward(request, response, "mybook/add.jsp");
	}
	
	/*
	 mybook?action=save
	 */
	public void actionSave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		log.debug("action save...");
		String type=this.getString(request, "type");
		int catogeryId=this.getInt(request, "catogeryId");
		Date recordDate=this.getDate(request, "recordDate");
		int amount=this.getInt(request, "amount");
		String remark=this.getString(request, "remark");
		log.debug("type:{} ,catogeryId:{},recordDate:{},amount:{},remark:{}",
				type,catogeryId,recordDate,amount,remark);
		Detail d=new Detail(type, catogeryId,remark, amount, recordDate,
				getCurUserId(request), new Date());
		log.debug("new detail:"+d.toString());
		if(detailDao.addDetail(d)){
			this.redirect(response, "mybook?action=list");
		}else{
			throw new WebAppBusinessException("add a detial error");
		}
		
	}
	//http://localhost:8080/mybook?action=edit&id=1
	public void actionEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		log.debug("action edit...");
		int id=this.getInt(request, "id");
		Detail d=detailDao.findDetailById(id);
		log.debug("id={} detail={}",id,d);
		request.setAttribute("Detail", d);
		this.forward(request, response, "mybook/edit.jsp");
	}
	public void actionUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		log.debug("action update...");
		int id=this.getInt(request, "id");
		String type=this.getString(request, "type");
		int catogeryId=this.getInt(request, "catogeryId");
		Date recordDate=this.getDate(request, "recordDate");
		int amount=this.getInt(request, "amount");
		String remark=this.getString(request, "remark");
		if(detailDao.updateDetail(id, type, catogeryId, 
				recordDate, amount, remark, getCurUserId(request))){
			this.redirect(response, "mybook?action=list");
		}else{
			throw new WebAppBusinessException("update a detial error");
		}
	}
	public void actionDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		log.debug("action del...");
		int id=this.getInt(request, "id");
		if(detailDao.delDetailById(id)){
			this.redirect(response, "mybook?action=list");
		}else{
			throw new WebAppBusinessException("del a detial error");
		}
	}
	private int getCurUserId(HttpServletRequest request){
		HttpSession session=request.getSession();
		User curUser=(User)session.getAttribute(MybookConstant.SESSION_NAME_CUR_USER);
		return curUser.getId();
	}
	
	
	
	
	
	
	
	
	
}
